public class Solution807 {
    public int maxIncreaseKeepingSkyline(int[][] grid) {
        int[] x=new int[grid.length];
        int[] y=new int[grid[0].length];
        for (int i=0;i<grid.length;i++){
            for (int j=0;j<grid[i].length;j++){
                x[i]=Math.max(x[i],grid[i][j]);
            }
        }
        for (int i=0;i<grid[0].length;i++){
            for (int j=0;j<grid.length;j++){
                y[i]=Math.max(y[i],grid[j][i]);
            }
        }
        int ans=0;
        for (int i=0;i<grid.length;i++){
            for (int j=0;j<grid[i].length;j++){
                ans+=Math.min(x[i],y[j])-grid[i][j];
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution807().maxIncreaseKeepingSkyline(new int[][]{{3,0,8,4},{2,4,5,7},{9,2,6,3},{0,3,1,0}}));
    }
}
